/**
 * Created by moxiaobei on 2017/2/28.
 */

var mySwiper1, mySwiper2, currentIndex;


$(document).ready(function() {
    mySwiper1 = new Swiper('.swiper-container', {
        pagination: '.pagination',
        paginationClickable: true,
        slidesPerView: 5, //可视窗口的数量
        offsetPxAfter: 50, //最后一个距离处理
        watchActiveIndex: true,
        scrollbar: '.swiper-scrollbar',
    });

});

$("header .swiper-slide").each(function() {
    $(this).click(function() {
        currentIndex = $(this).index();
        changeTab(currentIndex);
    })

});

//tab内容页滑动效果
var startPosition, endPosition, deltaX, deltaY, moveLength;
var length = $(".m-con-item").length;
$(".m-con-item").bind('touchstart', function(e) {
    var touch = e.touches[0];
    startPosition = {
        x: touch.pageX,
        y: touch.pageY
    }
}).bind('touchmove', function(e) {
    var touch = e.touches[0];
    endPosition = {
        x: touch.pageX,
        y: touch.pageY
    };
    deltaX = endPosition.x - startPosition.x;
    deltaY = endPosition.y - startPosition.y;
    moveLength = Math.sqrt(Math.pow(Math.abs(deltaX), 2) + Math.pow(Math.abs(deltaY), 2));
}).bind('touchend', function(e) {
    if (deltaX < 0) { // 向左划动  
        // console.log("向左划动" + deltaX);
        if (deltaX < -116) {
            if ($(this).index() < (length - 1))
                changeTab($(this).index() + 1)
        }
    } else if (deltaX > 0) { // 向右划动  
        // console.log("向右划动");
        if (deltaX > 16) {
            if ($(this).index() > 0)
                changeTab($(this).index() - 1)
        }
    }
});


function changeTab(currentIndex) {
    $("header .swiper-slide").removeClass("active");
    $("header .swiper-slide").eq(currentIndex).addClass("active");
    mySwiper1.swipeTo(currentIndex, 1000, true);
    $(".m-con-item").removeClass("active");
    $(".m-con-item").hide();
    $(".m-con-item").eq(currentIndex).fadeIn();
}